//
//  HomeExhibitTitleView.m
//  ArtDog
//
//  Created by dllo on 16/9/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HomeExhibitTitleView.h"
#import "Masonry.h"
#import "UIColor+Common.h"
#import "HLMacro.h"

#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height


@interface HomeExhibitTitleView ()

@property (nonatomic, strong) UIButton *exhibitBtn;

@property (nonatomic, strong) UIButton *institutionBtn;

@property (nonatomic, strong) UIView *lineView;

@end

@implementation HomeExhibitTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
        [self layoutAllViews];
        
    }
    return self;
}

- (void)setTitles:(NSArray *)titles
{
    _titles = titles;
    [_exhibitBtn setTitle:_titles[0] forState:UIControlStateNormal];
    [_institutionBtn setTitle:_titles[1] forState:UIControlStateNormal];
}

- (void)setupViews
{
    _exhibitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_exhibitBtn];
    _exhibitBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_exhibitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [_exhibitBtn addTarget:self action:@selector(firstAction) forControlEvents:UIControlEventTouchUpInside];
    
    _institutionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_institutionBtn];
    _institutionBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_institutionBtn setTitleColor:[UIColor colorFromHexRGB:@"8a8a8a"] forState:UIControlStateNormal];
    [_institutionBtn addTarget:self action:@selector(secondAction) forControlEvents:UIControlEventTouchUpInside];
    
    _lineView = [[UIView alloc]initWithFrame:CGRectMake(WIDTH / 4 - 17, HEIGHT - 1, 34, 1)];
    [self addSubview:_lineView];
    _lineView.backgroundColor = [UIColor blackColor];
}

- (void)layoutAllViews
{
    [_exhibitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.centerX.equalTo(self).multipliedBy(0.5);
    }];
    [_institutionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.centerX.equalTo(self).multipliedBy(1.5);
    }];
}

#pragma mark - button clicked
- (void)firstAction
{
    if (_lineView.frame.origin.x != WIDTH / 4 - 17) {
        [_exhibitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_institutionBtn setTitleColor:[UIColor colorFromHexRGB:@"8a8a8a"] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            _lineView.frame = CGRectMake(WIDTH / 4 - 17, HEIGHT - 1, 34, 1);
        } completion:nil];
        if (_collectionViewOffset) {
            _collectionViewOffset(0);
        }
    }
}

- (void)secondAction
{
    if (_lineView.frame.origin.x != 3 * WIDTH / 4 - 17) {
        [_institutionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_exhibitBtn setTitleColor:[UIColor colorFromHexRGB:@"8a8a8a"] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            _lineView.frame = CGRectMake(3 * WIDTH / 4 - 17, HEIGHT - 1, 34, 1);
        } completion:nil];
        if (_collectionViewOffset) {
            _collectionViewOffset(SCREEN_WIDTH);
        }
    }
}

@end
